package com.sky.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.result.Result;
import com.sky.service.IShoppingCartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 购物车 服务实现类
 * </p>
 *
 * @author fcy
 * @since 2025-09-11
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements IShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 添加购物车
     * @param shoppingCartDTO
     * @return
     */
    @Override
    public Result<String> addCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        ShoppingCart one = lambdaQuery()
                .eq(ShoppingCart::getUserId,shoppingCart.getUserId())
                .eq(shoppingCartDTO.getDishId()!=null ,ShoppingCart::getDishId, shoppingCartDTO.getDishId())
                .eq(shoppingCartDTO.getSetmealId()!=null,ShoppingCart::getSetmealId, shoppingCartDTO.getSetmealId())
                .eq(shoppingCartDTO.getDishFlavor()!=null,ShoppingCart::getDishFlavor, shoppingCartDTO.getDishFlavor())
                .one();
        if(one != null){
            one.setNumber(one.getNumber()+1);
            updateById(one);
            return Result.success("新增成功");
        }
        //判断添加的是菜品还是套餐
        if(shoppingCartDTO.getDishId() != null){
            Dish dish = dishMapper.selectById(shoppingCartDTO.getDishId());
            shoppingCart.setName(dish.getName());
            shoppingCart.setImage(dish.getImage());
            shoppingCart.setAmount(dish.getPrice());
        }
        if(shoppingCartDTO.getSetmealId() != null){
            Setmeal setmeal=setmealMapper.selectById(shoppingCartDTO.getSetmealId());
            shoppingCart.setName(setmeal.getName());
            shoppingCart.setImage(setmeal.getImage());
            shoppingCart.setAmount(setmeal.getPrice());
        }
        shoppingCart.setCreateTime(LocalDateTime.now());
        shoppingCart.setNumber(1);
        save(shoppingCart);
        return Result.success();

    }
    /**
     * 查看购物车
     * @return
     */
    @Override
    public Result<List<ShoppingCart>> queryList() {
        Long userId = BaseContext.getCurrentId();
        List<ShoppingCart> list = lambdaQuery()
                .eq(ShoppingCart::getUserId, userId)
                .list();
        return Result.success(list);
    }
    /**
     * 清空购物车
     * @return
     */
    @Override
    public Result<String> clean() {
        Long userId = BaseContext.getCurrentId();
        boolean remove = lambdaUpdate()
                .eq(ShoppingCart::getUserId, userId)
                .remove();
        return remove ? Result.success() : Result.error("清空购物车失败");
    }
    /**
     * 删减购物车
     * @param shoppingCartDTO
     * @return
     */
    @Override
    public Result<String> sub(ShoppingCartDTO shoppingCartDTO) {
        //获取用户id
        //删减的是菜品
        if(shoppingCartDTO.getDishId() != null){
            ShoppingCart one = lambdaQuery()
                    .eq(ShoppingCart::getUserId, BaseContext.getCurrentId())
                    .eq(ShoppingCart::getDishId, shoppingCartDTO.getDishId())
                    .eq(shoppingCartDTO.getDishFlavor()!=null, ShoppingCart::getDishFlavor, shoppingCartDTO.getDishFlavor())
                    .one();
            if(one != null){
                if(one.getNumber() == 1){
                    shoppingCartMapper.deleteById( one.getId());
                }else{
                    one.setNumber(one.getNumber()-1);
                    updateById(one);
                }
            }
        }
        //删减的是套餐
        if(shoppingCartDTO.getSetmealId() != null){
            ShoppingCart one = lambdaQuery()
                    .eq(ShoppingCart::getUserId, BaseContext.getCurrentId())
                    .eq(ShoppingCart::getSetmealId, shoppingCartDTO.getSetmealId())
                    .one();
            if(one != null){
                if(one.getNumber() == 1){
                    removeById(one);
                }else{
                    one.setNumber(one.getNumber()-1);
                    updateById(one);
                }
            }
        }
        return Result.success();
    }
}
